<?php

class Station extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Mstation');
        $this->load->model('Mline');
    }

    public function index() {
        //Pre-load View
        $this->_data['load_page'] = 'station/index_view';
        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Station[name]', 'Station name', 'required');
            if ($this->form_validation->run()) {
                $station = $this->input->post('Station');
                $this->Mstation->insertStation($station);
                $this->session->set_flashdata('flash_mess', 'Create Station Successfully.');
                redirect(base_url('station/manage'));
            } else {
                $this->_data['data']['error'] = validation_errors('<li>','</li>');
            }
        }
        //Load View        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function display($page="0") {
        $this->_data['load_page'] = 'station/display_view';
        //Pagination   
        $config['base_url'] = base_url('station/display/');
        $config['total_rows'] = $this->Mstation->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        $this->_data['data']['station'] = $this->Mstation->listAllStations($config['per_page'], $page, 'id desc');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function manage($page="0") {
        //Pre-load View
        $this->_data['load_page'] = 'station/manage_view'; 
        //Pagination   
        $config['base_url'] = base_url('station/manage/');
        $config['total_rows'] = $this->Mstation->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        //echo '<pre>';
        //print_r($station);echo'</pre>';exit();
        $this->_data['current_page'] = $page;
        $this->_data['data']['station'] = $this->Mstation->listAllStations($config['per_page'], $page, 'id desc');  
        $this->_data['flash_mess'] = $this->session->flashdata('flash_mess');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function update($id) {
        //Pre-load View
        $this->_data['load_page']= 'station/update_view';
        $this->_data['data'] = $this->Mstation->getStationById($id);
        $this->_data['data_id'] = $id;
        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Station[name]', 'Station name', 'required');
            if ($this->form_validation->run()) {
                $data_update = $this->input->post('Station');
                $this->Mstation->updateStation($data_update, $id);
                $this->session->set_flashdata('flash_mess', 'Update Station Successfully.');
                redirect(base_url('station/manage'));
                exit();
            } else {
                $this->_data['data']['error'] = validation_errors('<li>','</li>');
            }  
        }
        //Load View        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function delete($id) {
        $this->Mstation->deleteStation($id);
        redirect(base_url('station/manage'));
    }
        
}

?>